package com.nx.schedule;

import com.nx.Constant;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.common.message.Message;

/**
 * 1、producer 发送的topic是nx-user-delay
 *    message.setDelayTimeLevel(3); //level==0非延时消息
 * 2、MQ接收到到msg,把msg替换为sechedule_topic_nx-user-delay
 * 3、达到了延迟的要求后 恢复成真正的主题nx-user-delay
 */
public class ScheduledMessageProducer {
    public static void main(String[] args) throws Exception {

        DefaultMQProducer producer = new DefaultMQProducer("nx-schedule-group");
        producer.setSendMsgTimeout(10*1000);
        producer.setNamesrvAddr(Constant.MQ);

        producer.start();
        int totalMessagesToSend = 3;
        for (int i = 0; i < totalMessagesToSend; i++) {
            Message message = new Message("nx-user-delay", ("Hello scheduled message " + i).getBytes());
            //延时消费 1s 5s 10s 30s 1m 2m 3m 4m 5m 6m 7m8m 9m 10m 20m 30m 1h 2h
            message.setDelayTimeLevel(3);
            // Send the message
            producer.send(message);
        }

        System.out.printf("message send is completed .%n");
        producer.shutdown();
    }
}
